<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/29
 * Time: 下午8:20
 */

class ShopController extends AdminBaseController {
	
	public function oneAnalysis(){
		$this->setTitle('商城单个物品统计');
	
		$filter = new PubFilter();
		$chart  = new PubChart();
		$grid   = new PubGrid();
	
		$lod_special_id = $this->R('special_id');
		$lodos = $this->R('os');
		$filter->addSpecialFilter(false,2);
		$filter->addOsFilter($lod_special_id,true,2)
		->addServerFilter($lod_special_id,$lodos,true,false,2)
		->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-7 day'))),2)
		->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))),2)
		->addExport();
		$filter->addCoin(true);
	
		$params = $filter->get();
	
		$params = $this->indexCommon($params);
	
		$header = ShopService::oneHeader($params);
		$data   = ShopService::oneData($params);
	
		if($filter->isExport()){
			$filename = LibExcel::getFilename('商城单个物品统计', $params);
			LibExcel::exportCsv($header, $data, $filename);
		}else{
			$grid->addGrid($header, $data, array(
					'length' => 50
			));
	
			$this->out['filter'] = $filter->show();
			$this->out['chart']  = $chart->show();
			$this->out['grid']   = $grid->show();
			$this->out['is_ana'] = 2;
		}
	}
	
	
	public function allAnalysis(){
		$this->setTitle('商城所有物品统计');
	
		$filter = new PubFilter();
		$chart  = new PubChart();
		$grid   = new PubGrid();
	
		$lod_special_id = $this->R('special_id');
		$lodos = $this->R('os');
		$filter->addSpecialFilter(false,2);
		$filter->addOsFilter($lod_special_id,true,2)
		->addServerFilter($lod_special_id,$lodos,true,false,2)
		->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-7 day'))),2)
		->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))),2)
		->addExport();
		$filter->addCoin(true);
	
		$params = $filter->get();
	
		$params = $this->indexCommon($params);
	
		$header = ShopService::allHeader($params);
		$data   = ShopService::allData($params);
	
		if($filter->isExport()){
			$filename = LibExcel::getFilename('商城所有物品统计', $params);
			LibExcel::exportCsv($header, $data, $filename);
		}else{
			$grid->addGrid($header, $data, array(
					'length' => 50
			));
	
			$this->out['filter'] = $filter->show();
			$this->out['chart']  = $chart->show();
			$this->out['grid']   = $grid->show();
			$this->out['is_ana'] = 2;
		}
	}
	

    /**
     * 商城购买报表
     */
    public function index(){
        $this->setTitle('商城购买');

        $filter = new PubFilter();
        $chart  = new PubChart();
        $grid   = new PubGrid();

        $coinTypes = CoinService::getCoinType();
        $filter->addOsFilter()
            ->addServerFilter()
            ->addFilter('date', 'input', PubFunc::t('日期'), array('value' => date('Y-m-d',strtotime('yesterday')), 'date' => 'day'))
            ->addFilter('coinType', 'select', PubFunc::t('货币'), array('options' => $coinTypes, 'value' => key($coinTypes)))
            ->addFilter('state', 'hidden', '', array('value' => 'ALL'))
            ->addExport();
        $params = $filter->get();

        $header = ShopService::indexHeader();

        $data   = ShopService::indexData($params,array("CoinType"=>$coinTypes));

        if($filter->isExport()){
            $filename = LibExcel::getFilename('商城购买', $params);
            LibExcel::exportCsv($header, $data['data'], $filename);
        }else{
            $chartConfig = ShopService::indexChart($data['data'], $params);
            $chart->addChart('chart', $chartConfig['data'], array(
                'name'   => "购买数量",
                'type'   => 'bar',
                'config'=>array(
                    'xAxis' => array('axisLabel' => array('rotate' => 40)),
                ),
            ));

            $grid->addGrid($header, $data, array(
                'order' => array('UsedCoin', 'desc'),
            ));

            $this->out['filter'] = $filter->show();
            $this->out['chart'] = $chart->show();
            $this->out['grid'] = $grid->show();
        }
    }

    /**
     * 神秘商店购买报表
     */
    public function black(){
        $this->setTitle('神秘商店');

        $filter = new PubFilter();
        $chart  = new PubChart();
        $grid   = new PubGrid();

        $coinTypes = CoinService::getCoinType();
        $filter->addOsFilter()
            ->addServerFilter()
            ->addFilter('date', 'input', PubFunc::t('日期'), array('value' => date('Y-m-d',strtotime('yesterday')), 'date' => 'day'))
            ->addFilter('coinType', 'select', PubFunc::t('货币'), array('options' => $coinTypes, 'value' => key($coinTypes)))
            ->addFilter('state', 'hidden', '', array('value' => 'ALL'))
            ->addExport();
        $params = $filter->get();

        $header = ShopService::blackHeader();

        $data   = ShopService::blackData($params,array("CoinType"=>$coinTypes));

        if($filter->isExport()){
            $filename = LibExcel::getFilename('神秘商店', $params);
            LibExcel::exportCsv($header, $data['data'], $filename);
        }else{
            $chartConfig = ShopService::indexChart($data['data'], $params);
            $chart->addChart('chart', $chartConfig['data'], array(
                'name'   => "神秘商店购买数量",
                'type'   => 'bar',
                'config'=>array(
                    'xAxis' => array('axisLabel' => array('rotate' => 40)),
                ),
            ));

            $grid->addGrid($header, $data, array(
                'order' => array('UsedCoin', 'desc'),
            ));

            $this->out['filter'] = $filter->show();
            $this->out['chart'] = $chart->show();
            $this->out['grid'] = $grid->show();
        }
    }

    public function analysis(){
        $this->setTitle('商城购买数据分析');

        $filter = new PubFilter('pl');
        $grid   = new PubGrid();

        $filter->addOsFilter()
            ->addServerFilter(true)
            ->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-31 day'))))
            ->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))))
            ->addExport()
        ;

        $params = $filter->get();
        $params['period'] = 'Daily';

        $info = ShopService::analysisInfo($params);
        $header = $info['header'];
        $data   = $info['data'];

        if($filter->isExport()) {
            $filename = LibExcel::getFilename('', $params);
            LibExcel::exportCsv($header, $data, $filename);
        }else{
            $grid->addGrid($header, $data, array(
                'name'  => '商城购买数据分析',
                'order' => array('RecDate', 'desc')
            ));

            $this->out['filter']    = $filter->show();
            $this->out['grid']      = $grid->show();
        }
    }
} 
